from flask import Flask, abort,jsonify
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_restful import reqparse, Api, Resource, marshal_with
from flask_sqlalchemy import SQLAlchemy
from operator import or_, and_
import settings
import flask_restful
from flask_httpauth import HTTPBasicAuth
from src.common.code import make_result, Code

app = Flask(__name__, static_url_path='')
CORS(app)
app.config.from_object("settings.FlaskSetting")  # 导入app的配置信息。

auth = HTTPBasicAuth()
jwt = JWTManager(app)  # 实例化jwt
db = SQLAlchemy(app)  # 实例化db。


def custom_abord(http_status_code, *args, **kwargs):
    # 只要http_status_code 为400， 报参数错误
    if http_status_code == 400:
        abort(400,**make_result(code=Code.PARAM_FAIL))
    # 正常返回消息
    abort(http_status_code)


flask_restful.abort = custom_abord

from models import Student, User  # 需要把创建的数据库类导入，不然创建表会失败。

from src.module.student import student
from src.module.user import user

app.register_blueprint(student)  # 注册蓝图
app.register_blueprint(user)

if __name__ == '__main__':
    app.run(debug=True)
